package com.lanyou.esb.cook.proxy.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import redis.clients.jedis.exceptions.JedisException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lanyou.esb.cook.proxy.common.DateUtils;
import com.lanyou.esb.cook.proxy.entity.IfAlrReqTimes;
import com.lanyou.esb.cook.proxy.meta.TimeUnit;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.IfAlrReqTimesJedisProxy;
import com.lanyou.esb.cook.proxy.redis.vo.Group;
import com.lanyou.esb.cook.proxy.redis.vo.Group.Entry;
import com.lanyou.esb.cook.proxy.redis.vo.RedisActionType;
import com.lanyou.esb.cook.proxy.service.GroupService;
import com.lanyou.esb.cook.proxy.service.IfAlrReqTimesService;

/**
 * 接口已请求次数服务
 * 
 * @author Davey.wu
 */
public class IfAlrReqTimesServiceImpl implements IfAlrReqTimesService,
		GroupService<IfAlrReqTimes> {

	/** Redis代理接口 **/
	private IfAlrReqTimesJedisProxy jedisProxy;

	@Override
	public Group getGroup(IfAlrReqTimes ifAlrReqTimes) {
		if (ifAlrReqTimes == null) {
			return null;
		}

		Group group = new Group();
		// string类型条件集合
		List<Entry> strs = new ArrayList<>(1);
		group.setStrs(strs);

		Entry entry = new Entry();
		entry.setKey(jedisProxy.getKeyFromFields(ifAlrReqTimes.getIfId(),
				ifAlrReqTimes.getUnit().ValueCode,
				ifAlrReqTimes.getCallSystemCode(), ifAlrReqTimes.getSection()));
		entry.setValue(1 + "");
		entry.setTimeOut(jedisProxy.getTimeOut());
		entry.setType(RedisActionType.INCR_BY);
		strs.add(entry);

		return group;
	}

	@Override
	public void pushToRedis(IfAlrReqTimes ifAlrReqTimes) {
		if (ifAlrReqTimes == null) {
			return;
		}
		try {
			jedisProxy.incrBy(jedisProxy.getKeyFromFields(
					ifAlrReqTimes.getIfId(), ifAlrReqTimes.getUnit().ValueCode,
					ifAlrReqTimes.getCallSystemCode(),
					ifAlrReqTimes.getSection()), 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public IfAlrReqTimes get(String ifId, TimeUnit unit, String callSystemCode,
			Date requestDate) {
		IfAlrReqTimes reqTimes = null;
		String timeSection = getSection(unit, requestDate);
		try {
			reqTimes = jedisProxy.get(jedisProxy.getKeyFromFields(ifId,
					unit.ValueCode, callSystemCode, timeSection));
			// 没有取得对象，
			if (reqTimes == null) {
				reqTimes = create(ifId, unit, callSystemCode, requestDate);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return reqTimes;
	}

	@Override
	public IfAlrReqTimes create(String ifId, TimeUnit unit,
			String callSystemCode, Date requestDate) throws JedisException,
			JsonProcessingException {
		IfAlrReqTimes reqTimes = new IfAlrReqTimes();
		reqTimes.setIfId(ifId);
		reqTimes.setUnit(unit);
		reqTimes.setCallSystemCode(callSystemCode);
		reqTimes.setTimes(0);
		reqTimes.setSection(getSection(unit, requestDate));
		return reqTimes;
	}

	/**
	 * 获取时间区间
	 * 
	 * @param unit
	 *            ：时间单位
	 * @param date
	 *            ：日期
	 * @return
	 */
	public static String getSection(TimeUnit unit, Date date) {
		switch (unit) {
		case DAY:
			// 以天为单位，取当天的0点0分0秒
			return formate(date, DateUtils.DATEPATTERN_YYYYMMDD);
		default:
			return "";
		}
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 *            ：日期
	 * @param format
	 *            ：格式模式
	 * @return
	 */
	private static String formate(Date date, SimpleDateFormat format) {
		if (date == null || format == null) {
			return "";
		}
		return format.format(date);
	}

	public IfAlrReqTimesJedisProxy getJedisProxy() {
		return jedisProxy;
	}

	public void setJedisProxy(IfAlrReqTimesJedisProxy jedisProxy) {
		this.jedisProxy = jedisProxy;
	}

}
